﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using InHD.model;
using System.Windows.Input;
using InHD.dataaccess;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using InHD.commands;
using System.Windows;
using InHD.Properties;
using InHD.utilities.enums;

namespace InHD.viewmodels
{
    class StaffViewModel: WorkspaceViewModel
    {
        #region Constructor
        public StaffViewModel()
        {
            this._staffRepository = new StaffRepository();
            this._staff = Staff.CreateNewStaff();
            LoadStaffList();
            this.PageContent = new StaffListViewModel(this._staffRowCollection);
            this.PageContent.RequestClose += this.HandleCloseCurrentPage;
        }
        #endregion

        #region Value Property
        private Staff _staff;
        readonly StaffRepository _staffRepository;
        private WorkspaceViewModel _pageContent;
        private ObservableCollection<Staff> _staffRowCollection = new ObservableCollection<Staff>();
        private int _rowIndex = -1;
        #endregion

        #region Presentation Property

        public WorkspaceViewModel PageContent
        {
            get
            {
                return _pageContent;
            }
            set
            {
                _pageContent = value;
            }
        }

        #endregion

        #region Normal Method
        private void LoadStaffList()
        {
            this._staffRepository.LoadAllStaffs();
            this._staffRowCollection = this._staffRepository.GetStaffs();
        }

        private void HandleCloseCurrentPage(object source, EventArgs e)
        {
            this.PageContent.RequestClose -= this.HandleCloseCurrentPage;
            if (this.PageContent is StaffListViewModel)
            {
                string commandExcuted = ((StaffListViewModel)PageContent).commandExcute;
                if (string.IsNullOrEmpty(commandExcuted))
                {
                    OnRequestClose();
                    return;
                }

                if (commandExcuted.Equals("View"))
                {
                    this._staff = ((StaffListViewModel)PageContent).SelectedStaff.CreateShallowCopy();
                    this.PageContent = new StaffFormViewModel(ActionModes.Edit, _staff);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Add"))
                {
                    this._staff = Staff.CreateNewStaff();
                    this.PageContent = new StaffFormViewModel(ActionModes.New, _staff);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
            }
            else if (this.PageContent is StaffFormViewModel)
            {
                string commandExcuted = ((StaffFormViewModel)PageContent).commandExcute;
                if (commandExcuted.Equals("Close"))
                {
                    this._staff = null;
                    this.PageContent = new StaffListViewModel(_staffRowCollection);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Save"))
                {
                    if (((StaffFormViewModel)PageContent).FormMode.Equals(ActionModes.New))
                    {
                        string result = this._staffRepository.AddStaff(((StaffFormViewModel)PageContent).Staff);
                        if (string.IsNullOrEmpty(result))
                        {
                            this._staffRowCollection = null;
                            this._staffRowCollection = _staffRepository.GetStaffs();
                            this.PageContent = new StaffListViewModel(_staffRowCollection);
                        }
                        else if (result.Equals("DuplicateCode"))
                        {
                            MessageBox.Show(Resources.InHD_Error_DuplicateCode);
                            this.PageContent = new StaffFormViewModel(ActionModes.New, ((StaffFormViewModel)PageContent).Staff);
                        }
                        else
                        {
                            MessageBox.Show(Resources.InHD_DB_Error);
                            this.PageContent = new StaffFormViewModel(ActionModes.New, ((StaffFormViewModel)PageContent).Staff);
                        }
                    }
                    else
                    {
                        string result = this._staffRepository.EditStaff(((StaffFormViewModel)PageContent).Staff);
                        if (string.IsNullOrEmpty(result))
                        {
                            this._staffRowCollection = null;
                            this._staffRowCollection = _staffRepository.GetStaffs();
                            this.PageContent = new StaffListViewModel(_staffRowCollection);
                        }
                        else if (result.Equals("DuplicateCode"))
                        {
                            MessageBox.Show(Resources.InHD_Error_DuplicateCode);
                            this.PageContent = new StaffFormViewModel(ActionModes.Edit, ((StaffFormViewModel)PageContent).Staff);
                        }
                        else
                        {
                            MessageBox.Show(Resources.InHD_DB_Error);
                            this.PageContent = new StaffFormViewModel(ActionModes.Edit, ((StaffFormViewModel)PageContent).Staff);
                        }
                    }

                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Delete"))
                {
                    if (!string.IsNullOrEmpty(_staffRepository.DeleteStaff(((StaffFormViewModel)PageContent).Staff.StaffUNK)))
                    {
                        MessageBox.Show(Resources.InHD_DB_Error);
                    }
                    else
                    {
                        this._staffRowCollection = null;
                        this._staffRowCollection = _staffRepository.GetStaffs();
                    }

                    this.PageContent = new StaffListViewModel(_staffRowCollection);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
            }
        }
        #endregion

        public override string DisplayName
        {
            get
            {
                try
                {
                    return Resources.Title_Staff;
                }
                catch(Exception ex)
                {
                    return string.Empty;
                }
            }

        }
    }
}
